package com.data.unityplugin_market.viewmodel

import android.content.Context
import android.databinding.ObservableField
import com.data.unityplugin_market.base.BaseVM
import com.data.unityplugin_market.config.HttpConfig
import com.data.unityplugin_market.model.req.ReqDeleteGoods
import com.data.unityplugin_market.model.req.ReqModifyPwd
import com.data.unityplugin_market.network.NetWorkConfig
import com.data.unityplugin_market.network.RetrofitService
import com.data.unityplugin_market.util.ToastUtil
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

public class SystemSettingFragmetVM : BaseVM{

    /**
     *
     */
    var modifyPwdVisibility = ObservableField<Boolean>(false)

    /**
     *
     */
    var logoutVisibility = ObservableField<Boolean>(false)

    /**
     *
     */
    var oldPwd = ObservableField<String>()

    /**
     *
     */
    var newPwd = ObservableField<String>()

    /**
     *
     */
    var accountNumber = ObservableField<String>("")

    /**
     *
     */
    var context : Context ? = null

    constructor(context : Context) {
        this.context = context;
        accountNumber.set(HttpConfig.PHONE_ID)
    }

    fun submitModifyPwd() {

        if (!newPwd.get().equals(oldPwd.get())){
            ToastUtil.showToast("两次密码不同",context!!)
            return;
        } else {
            var req = ReqModifyPwd()
            req.channel = "1"
            req.password = newPwd.get()!!
            req.id = HttpConfig.PHONE_ID

            NetWorkConfig.instance.retrofit.create(RetrofitService::class.java)
                    .modifyPwd(req, HttpConfig.MODIFY_PWD, HttpConfig.getBaseKeyMap())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread()).doOnSubscribe {}
                    .subscribe({
                        ToastUtil.showToast("修改成功",context!!)
                        modifyPwdVisibility.set(false)
                    }, {

                    })
        }
    }

    fun logout(){

    }

    fun modifyPwdClick() {
        modifyPwdVisibility.set(modifyPwdVisibility.get() != true)
        logoutVisibility.set(false)
    }


    fun logoutClick() {

        logoutVisibility.set(logoutVisibility.get() != true)

        modifyPwdVisibility.set(false)
    }
}